package wang.jinggo.cryptto;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * 发送者通过私钥加密数据DATA，并输出DATA 和签名后的DATA
 * @author wangyj
 * @description
 * @create 2017-06-26 9:47
 **/
public class SignatureData {

    private final static Logger LOG =  LoggerFactory.getLogger(SignatureData.class);

    public void run() {
        String prikeyvalue = "30820277020100300d06092a864886f70d0101010500048202613082025d0201000281810085e6ff982b5a540003657a8b48f6375e0464efed024ecd2b79d8b605e2337bf8716b5b365e88682d0c814354e2a2fefdb9a5b16869deeb3e985341ecf6bb2adddfe9f0539ec8afea349eb3552f2ff81ac88772a316acac9bce0fb76b524cc64778d19375816409730e481d369e42a53763ac0d7ac50d7e0ef6939325b21db075020301000102818036b9d762b8ed3d019861c438e3040652fd96c75c65a21146a0910aabd2ec59d18b8166110d87d09400d55fd074a44b752587ebbf11ee12d54d6a51ba6888cc96b4247bff17a2b8c23724f17820ec38e3be7ea9568a345cf4b805f4b21ac9b73eb9153af19a0e0690f857b4a260aadf7ff7ee9f59415552ed9e8466c751a18a21024100c7a9e9e54e639d513b868abccbad814919502e9a4af3f3b6d9e78b8e5fe8b0c86b33075ed78ac4571ccf673612991bcdcbc374aef3627e07107dad50fa4b4039024100abaf041cca94af4deb0ff3c9f5cde4f8799a7e865e250e542f65e0b882398d69423e6d2213434e32ce4d7de3842aad63852b89181ecacbb41f4c0c59c65eba1d024100b09d01338002a79eba6bc97f351f75f4263b01ee238ce531025149e617ba69ddeecd45a0c44290688a2880209994ebbe3f96d639073b41459875c3009556492902407ba7b3fb94c1a0be2b57330d3cb8ce795504bcaac4f645aac6f4c6f5bdfdfbe75b6a212a90db42607f8cfae63b148e0c0727c337973e1398298e99bab39118d5024100a33132e53a27f6b97b2521c79b4289c8994de6645ac1714a602ccf38ec9f41104ca5d4d9b8ee5d50e60ad16e56d87c5d7a55b80cf223eed9e7a28a3fe685a307";//这是GenerateKeyPair输出的私钥编码
        PKCS8EncodedKeySpec priPKCS8=new PKCS8EncodedKeySpec(hexStrToBytes(prikeyvalue));
        try {
            KeyFactory keyf= KeyFactory.getInstance("RSA");
            PrivateKey myprikey=keyf.generatePrivate(priPKCS8);
            String myinfo = "orderId=10dkfadsfksdkssdkd&amount=80&orderTime=20060509"; // 要签名的信息
            // 用私钥对信息生成数字签名
            Signature signature = Signature.getInstance("MD5withRSA");
            signature.initSign(myprikey);
            signature.update(myinfo.getBytes("ISO-8859-1"));
            byte[] signed = signature.sign(); // 对信息的数字签名

            LOG.info("signed(签名内容)原值={}", bytesToHexStr(signed));
            LOG.info("info（原值）={}", myinfo);
            LOG.info("签名并生成文件成功");
        } catch (Exception  e) {
            e.printStackTrace();
            LOG.error("生成密钥对失败",e.getMessage());
        }
    }

    /**
     * Transform the specified byte into a Hex String form.
     */
    public static final String bytesToHexStr(byte[] bcd) {
        StringBuffer s = new StringBuffer(bcd.length * 2);

        for (int i = 0; i < bcd.length; i++) {
            s.append(bcdLookup[(bcd[i] >>> 4) & 0x0f]);
            s.append(bcdLookup[bcd[i] & 0x0f]);
        }

        return s.toString();
    }

    /**
     * Transform the specified Hex String into a byte array.
     */
    public static final byte[] hexStrToBytes(String s) {
        byte[] bytes;

        bytes = new byte[s.length() / 2];

        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2),
                    16);
        }

        return bytes;
    }

    private static final char[] bcdLookup = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}
